package com.healthtop.service.impl;

import com.healthtop.common.Specification;
import com.healthtop.dao.XsGameCategoryDao;
import com.healthtop.dto.XsGameGategoryDto;
import com.healthtop.model.XsImg;
import com.healthtop.service.XsGameGategoryService;
import com.healthtop.model.XsGameCategory;
import com.healthtop.service.XsImgService;
import com.healthtop.utils.AliyunOSSClientUtil;
import com.healthtop.utils.UserUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.Date;
import java.util.List;


@Service
public class XsGameGategoryServiceImpl implements XsGameGategoryService {

    @Autowired
    private XsGameCategoryDao xsGameGategoryDao;

    @Autowired
    private XsImgService xsImgService;

    @Override
    public XsGameGategoryDto selectById(XsGameGategoryDto xsGameGategoryDto) {
        XsGameCategory xsGameGategory = xsGameGategoryDao.selectById(xsGameGategoryDto.getId());
        BeanUtils.copyProperties(xsGameGategory,xsGameGategoryDto);
        if(!ObjectUtils.isEmpty(xsGameGategory)&&xsGameGategory.getGameImage()!=0){
            XsImg xsImg = xsImgService.selectImgById(xsGameGategory.getGameImage());
            xsGameGategoryDto.setImage(xsImg.getImgUrl());
        }
        return xsGameGategoryDto;
    }

    @Override
    public void operate(XsGameGategoryDto xsGameGategoryDto) {
        XsGameCategory xsGameCategory = new XsGameCategory();
        BeanUtils.copyProperties(xsGameGategoryDto,xsGameCategory);
        if(!ObjectUtils.isEmpty(xsGameGategoryDto.getImage())){
            try {
                String img = AliyunOSSClientUtil.upload(xsGameGategoryDto.getImage());
                XsImg xsImg = new XsImg();
                xsImg.setImgUrl("https://jianghexing-oss.oss-cn-beijing.aliyuncs.com/"+img);
                xsImg.setCreateTime(new Date());
                xsImg.setCreateUser(UserUtils.getCurrentUser().getUid());
                xsImg.setDelFalg("0");
                xsImgService.insert(xsImg);
                xsGameCategory.setGameImage(xsImg.getId());
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        if(xsGameGategoryDto.getMode()== Specification.OperateMode.CREATE.index){
            Date date = new Date();
            xsGameCategory.setCreateTime(date);
            xsGameCategory.setUpdateTime(date);
            xsGameCategory.setDelTime(date);
            xsGameCategory.setCreateUser(UserUtils.getCurrentUser().getUid());
            xsGameGategoryDao.insert(xsGameCategory);
        }else if(xsGameGategoryDto.getMode()==Specification.OperateMode.UPDATE.index){
            xsGameCategory.setUpdateTime(new Date());
            xsGameCategory.setUpdateUser(UserUtils.getCurrentUser().getUid());
            if(ObjectUtils.isEmpty(xsGameGategoryDto.getImage())){
                xsGameCategory.setGameImage(null);
            }
            xsGameGategoryDao.update(xsGameCategory);
        }
    }

    @Override
    public void delete(XsGameGategoryDto xsGameGategoryDto) {
        XsGameCategory xsGameCategory = new XsGameCategory();
        xsGameCategory.setId(xsGameGategoryDto.getId());
        xsGameCategory.setDelFalg(xsGameGategoryDto.getDelFalg());
        xsGameCategory.setDelTime(new Date());
        xsGameGategoryDao.update(xsGameCategory);

    }

    @Override
    public List<XsGameCategory> selectAll() {
        return xsGameGategoryDao.selectAll();
    }
}
